package org.dromara.common.encrypt.core.encryptor

import org.dromara.common.encrypt.core.EncryptContext
import org.dromara.common.encrypt.enumd.AlgorithmType
import org.dromara.common.encrypt.enumd.EncodeType
import org.dromara.common.encrypt.utils.EncryptUtils.decryptBySm4
import org.dromara.common.encrypt.utils.EncryptUtils.encryptBySm4
import org.dromara.common.encrypt.utils.EncryptUtils.encryptBySm4Hex

/**
 * sm4算法实现
 *
 * @author 老马
 * @version 4.6.0
 * @updater LikeYouDo
 * @date 2025/1/9 10:20
 */
open class Sm4Encryptor(
    private val context: EncryptContext
) : AbstractEncryptor(context) {
    /**
     * 获得当前算法
     */
    override fun algorithm(): AlgorithmType =
        AlgorithmType.SM4

    /**
     * 加密
     *
     * @param value      待加密字符串
     * @param encodeType 加密后的编码格式
     */
    override fun encrypt(value: String?, encodeType: EncodeType?): String =
        if (encodeType == EncodeType.HEX) {
            encryptBySm4Hex(value, context.password)
        } else {
            encryptBySm4(value, context.password)
        }

    /**
     * 解密
     *
     * @param value      待加密字符串
     */
    override fun decrypt(value: String?): String =
        decryptBySm4(value, context.password)

}
